<?php


namespace App\Http\Controllers\Car;


use App\Http\Controllers\Controller;
use App\Models\CarModels\SanitationStaff;
use App\Models\OperationManagement\JobScope;
use App\Models\OperationManagement\ProjectAreas;
use App\Models\PersonInfoManagement\PersonInfo;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request;
use Validator;

/**
 * @group 运营管理->环卫运营->区域划分->区域人员配置
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class SanitationStaffController extends Controller
{
    /**
     * 环卫区域人员查看列表
     *
     * @queryParam employee string 对应人员信息
     * @queryParam type string 任务类型
     * @queryParam scope string 对应道路信息
     *
     * @bodyParam id int optional  对应道路id
     * @bodyParam employee int optional  人员id
     */
    public function index(Request $request)
    {
        $projectAreas = SanitationStaff::with(['scope','employee','type'])
        ->where(function ($q) use($request){
            $request->has('id') && $q->where('scope_id',$request->get('id'));
            $request->has('employee') && $q->where('employee_id',$request->get('employee'));
    })->get();
        return response()->json(encodeMsg($projectAreas,200));
    }
    /**
     * 环卫区域查看单个
     *
     * @queryParam employee string 对应人员信息
     * @queryParam type string 任务类型
     * @queryParam scope string 对应道路信息
     *
     */
    public function show($id)
    {
        //创建模型
        $projectAreas = SanitationStaff::with(['scope','employee','type'])->find($id);
        if($projectAreas == []) {
            return response()->json(encodeMsg(["message" => "没有该区域信息!"], 404));
        } else {
            return response()->json(encodeMsg($projectAreas,200));
        }
    }
    /**
     * 环卫区域添加
     *
     * @bodyParam employee_id int required 对应人员信息 外键
     * @bodyParam type int required 任务类型 外键
     * @bodyParam scope_id int required 对应道路信息 外键
     *
     */
    public function store(Request $request)
    {
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json(encodeMsg($validator->errors()->messages(),400),400);
        }
        try{
            $projectAreas = SanitationStaff::create($request->all());
            return response()->json(encodeMsg($projectAreas,200),200);
        }catch (QueryException $e){
            return response()->json(encodeMsg($e->getMessage(),500),500);
        }
    }
    /**
     * 环卫区域修改
     *
     * @bodyParam employee_id int optional 对应人员 外键
     * @bodyParam type int optional 任务类型 外键
     * @bodyParam road_areas string optional 对应道路面积
     * @bodyParam category string optional 对应道路类型
     * @bodyParam banci int optional 班次id
     *
     */
    public function update(Request $request,$id)
    {
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json(encodeMsg($validator->errors()->messages(),400),400);
        }
        $carTask = SanitationStaff::find($id);
        if ($carTask == []) {
            return response()->json(encodeMsg(["message" => "没有该道路信息!"], 404));
        }

        try{
            if ($request->has('banci')){
                $ban=PersonInfo::where('employee_id',$carTask['employee_id'])->first();
                $ban->banci_id =$request->get('banci');
                $ban->save();
            }
            if ($request->has('road_areas')){
                $area=JobScope::where('id',$carTask['scope_id'])->first();
                $area->road_areas = $request->get('road_areas');
                $area->save();
            }
            if ($request->has('category')){
                $area=JobScope::where('id',$carTask['scope_id'])->first();
                $area->category = $request->get('category');
                $area->save();
            }
            if ($request->has('type')){
                $carTask->type = $request->get('type');
                $carTask->save();
            }
            if ($request->has('employee_id')){
                $carTask->employee_id = $request->get('employee_id');
                $carTask->save();
            }
            return response()->json(encodeMsg($carTask,200),200);
        }catch (QueryException $e){
            return response()->json(encodeMsg($e->getMessage(),500),500);
        }
    }
    /**
     * 环卫区域删除
     *
     */

    public function destroy($pk){
        $data = SanitationStaff::find($pk);
        if($data == []){
            return response()->json(encodeMsg(["message"=>"没有环卫区域信息"],404),404);
        }
        $data->delete();
        return response()->json(encodeMsg(["message"=>"删除成功"],204),200);
    }
}